from flask import Blueprint
from ..common.classes import *
from ..common.storage import *
from ..common.utils import *

bp = Blueprint('lodger2', __name__)

def getLodger():
	if 'lodger' not in g:
		g.lodger = Lodger(DBStorage(request.blueprint), getWebIO)
	return g.lodger

@bp.route("/", methods=['GET'])
def index():
	return getLodger().output()


@bp.route("/edit/<int:id>", methods=['GET'])
def editItem(id):
	return getLodger().editItem(id)


@bp.route("/delete/<int:id>", methods=['GET'])
def deleteItem(id):
	return getLodger().deleteItem(id)


@bp.route("/process/<int:id>", methods=['POST'])
def processItem(id):
	return getLodger().processItem(id)


@bp.route("/api/", methods=['GET'])
def api_index():
	return getLodger().output()


@bp.route("/api/<int:id>", methods=['GET'])
def api_getItem(id):
	return getLodger().getItem(id)


@bp.route("/api/", methods=['POST'])
def api_addItem():
	return getLodger().processItem(0)


@bp.route("/api/<int:id>", methods=['PUT'])
def api_setItem(id):
	return getLodger().processItem(id)


@bp.route("/api/<int:id>", methods=['DELETE'])
def api_deleteItem(id):
	return getLodger().deleteItem(id)


@bp.route("/api/", methods=['DELETE'])
def api_clean():
	return getLodger().clean()


@bp.teardown_request
def teardown_lodger(ctx):
	getLodger().store()
